#include "Console.h"
#include <iostream>
#include <assert.h>

Console::Console(short width, short height)
{
	oHandle = GetStdHandle(STD_OUTPUT_HANDLE);
	iHandle = GetStdHandle(STD_INPUT_HANDLE);

	assert(oHandle != INVALID_HANDLE_VALUE);
	assert(iHandle != INVALID_HANDLE_VALUE);
	
	oldOutput = new ConsoleSettings(OUTPUT, oHandle);
	oldInput = new ConsoleSettings(INPUT, iHandle);

	assert(oldOutput != NULL);
	assert(oldInput != NULL);

	newOutput = new ConsoleSettings(OUTPUT, oHandle);
	newInput = new ConsoleSettings(INPUT, iHandle);

	assert(newOutput != NULL);
	assert(newInput != NULL);

	SetScreenSize(width, height);

	newOutput->Get();
}

Console::~Console()
{
	oldOutput->Set();
	oldInput->Set();

	delete oldOutput;
	delete oldInput;
	delete newOutput;
	delete newInput;
}

bool Console::SetScreenSize(short width, short height)
{
	COORD largest = GetLargestConsoleWindowSize(oHandle);
	
	newOutput->csbi.dwSize.X = (width > largest.X ? largest.X : width);
	newOutput->csbi.dwSize.Y = (height > largest.Y ? largest.Y : height);
	newOutput->csbi.srWindow.Left = 0;
	newOutput->csbi.srWindow.Top = 0;
	newOutput->csbi.srWindow.Right = newOutput->csbi.dwSize.X - 1;
	newOutput->csbi.srWindow.Bottom = newOutput->csbi.dwSize.Y - 1;

	return (newOutput->Set());
}

bool Console::isValidZoneName(char * name)
{
	// Test validity of zone name.
	return false;
}

Zone * Console::CreateZone(char * name, short left, short top, short right, short bottom)
{
	if(!isValidZoneName(name))
	{
		return 0;
	}
	else
	{
		// Initialize a new Zone object into the vector 'zones' and return a pointer to it.
		// Return 0 if the operation fails
		return 0;
	}
}

bool Console::DeleteZone(char * name)
{
	if(!isValidZoneName(name))
	{
		return false;
	}
	else
	{
		// Remove the zone with the name 'name' from the vector 'zones'
		// Return false if the operation fails
		return false;
	}
}

Zone * Console::GetZone(char * name)
{
	if(!isValidZoneName(name))
	{
		return 0;
	}
	else
	{
		// Find and return the pointer to the zone with the name 'name'.
		// Return 0 if the operation fails
		return 0;
	}
}